package easy;
/*
367. 有效的完全平方数
解题思路：二分法
 */
public class LeeCode367 {

  public boolean isPerfectSquare(int num) {
    int left = 0;
    int right = num;
    while (left <= right){
      int mid = (left + right)/2;
//      注意，不用long的话很大概率发生溢出导致时间超了
      long res = (long)mid * mid;
      if (res > num){
        right = mid - 1;
      }else if (res < num){
        left = mid + 1;
      }else {
        return true;
      }
    }
    return false;
  }

  public static void main(String[] args) {

    System.out.println(new LeeCode367().isPerfectSquare(16));
  }
}
